package com.ywc.tools.communica;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.ywc.tools.Books;
import com.ywc.tools.IBooksManager;
import com.ywc.tools.R;

public class CommunicaActivity extends AppCompatActivity {

    private YConnection yConnection;
    private AIDLConnection aidlConnection;

    private static class MessengerHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg) {
            Log.d("H","接收到消息了");
        }
    }
    private final Messenger clientMessenger=new Messenger(new MessengerHandler());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_communica);
        yConnection = new YConnection();
        aidlConnection = new AIDLConnection();
    }

    public void clickUi(View view) {
        switch (view.getId())
        {
            case R.id.one:
                Intent intent = new Intent(this,MessengerService.class);
                bindService(intent,yConnection, Context.BIND_AUTO_CREATE);
                break;
            case R.id.aidl:
                Intent intent1 = new Intent(this,AIDLService.class);
                bindService(intent1,aidlConnection, Context.BIND_AUTO_CREATE);
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(yConnection);
        unbindService(aidlConnection);
    }

    class YConnection implements ServiceConnection
    {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Messenger messenger = new Messenger(service);
            Message obtain = Message.obtain();
            Bundle bundle = new Bundle();
            obtain.setData(bundle);
            obtain.what=1;
            obtain.replyTo=clientMessenger;
            try {
                messenger.send(obtain);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }


    class AIDLConnection implements ServiceConnection
    {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            IBooksManager iBooksManager = IBooksManager.Stub.asInterface(service);
            Books books = new Books();
            try {
                iBooksManager.addBook(books);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}
